package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.Business;
import com.ruoyi.business.service.BusinessService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.SysDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础业务Controller
 * 
 * @author xx
 * @date 2020-03-16
 */
@Controller
@RequestMapping("/business/base")
public class BusinessController extends BaseController {
    private String prefix = "business/base";

    @Autowired
    private BusinessService businessService;
    @Autowired
    private SysDictDataService sysDictDataService;

    @RequiresPermissions("business:base:view")
    @GetMapping()
    public String base()
    {
        return prefix + "/base";
    }

    /**
     * 查询基础业务列表
     */
    @RequiresPermissions("business:base:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Business business) {
        startPage();
        if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
            business.setUserId(ShiroUtils.getUserId());
        }
        List<Business> list = businessService.selectBusinessList(business);
        return getDataTable(list);
    }

    /**
     * 导出基础业务列表
     */
    @RequiresPermissions("business:base:export")
    @Log(title = "基础业务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Business business) {
        List<Business> list = businessService.selectBusinessList(business);
        ExcelUtil<Business> util = new ExcelUtil<Business>(Business.class);
        return util.exportExcel(list, "基础业务", sysDictDataService.selectAll(Business.class));
    }

    /**
     * 新增基础业务
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        // 查询当前非管理员用户开通业务集合
        if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
            // 方便前端thymeleaf模版，故转为字符串集合。
            mmap.put("businessTypes", businessService.selectBusinessTypesByUserId(ShiroUtils.getUserId()).stream().map(integer -> integer + "").collect(Collectors.toList()));
            mmap.put("rechargeChannels", businessService.selectRechargeChannelsByUserId(ShiroUtils.getUserId()).stream().map(integer -> integer + "").collect(Collectors.toList()));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存基础业务
     */
    @RequiresPermissions("business:base:add")
    @Log(title = "基础业务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Business business) {
        return toAjax(businessService.insertBusiness(business));
    }

    /**
     * 修改基础业务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Business business = businessService.selectBusinessById(id);
        mmap.put("business", business);
        // 查询当前非管理员用户开通业务集合
        if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
            mmap.put("businessTypes", businessService.selectBusinessTypesByUserId(ShiroUtils.getUserId()).stream().map(integer -> integer + "").collect(Collectors.toList()));
            mmap.put("rechargeChannels", businessService.selectRechargeChannelsByUserId(ShiroUtils.getUserId()).stream().map(integer -> integer + "").collect(Collectors.toList()));
        }
        return prefix + "/edit";
    }

    /**
     * 修改保存基础业务
     */
    @RequiresPermissions("business:base:edit")
    @Log(title = "基础业务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Business business) {
        return toAjax(businessService.updateBusiness(business));
    }

    /**
     * 删除基础业务
     */
    @RequiresPermissions("business:base:remove")
    @Log(title = "基础业务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Business business) {
        business.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(businessService.deleteBusinessByIds(business));
    }

    /**
     * 修改状态
     * @param business
     * @return
     */
    @RequiresPermissions("business:base:open")
    @Log(title = "基础业务-修改业务状态", businessType = BusinessType.UPDATE)
    @PostMapping("changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Business business) {
        Business b = new Business();
        b.setId(business.getId());
        b.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(businessService.changeStatus(b));
    }

    /**
     * 校验业务编码唯一性
     * @param businessCode 业务编码
     * @return 0-唯一
     */
    @PostMapping("/checkBusinessCodeUnique")
    @ResponseBody
    public int checkBusinessCodeUnique(String businessCode) {
        return businessService.checkBusinessCodeUnique(businessCode);
    }

    /**
     * 跳转选择用户页面
     */
    @GetMapping("/selectUser")
    public String selectUser() {
        return prefix + "/selectUser";
    }
}
